import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'node:path';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default ({ mode }) => {
    const env = loadEnv(mode, process.cwd());
    console.log('env', env);

    return defineConfig({
        plugins: [
            vue(), 
            AutoImport({
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
        ],
        resolve: {
            alias: {
                '@': resolve(__dirname, 'src'),
            },
        },
        server: {
            host: '0.0.0.0',
            port: 5173,
            open: true,
            https: false,
            proxy: {
                '/api': {
                    target: env.VITE_PROXY,
                    changeOrigin: true,
                    ws: true,
                    rewrite: (path: string) => path.replace(/^\/api/, ''),
                },
            },
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: '@import "@/assets/styles/index.scss";@import "@/assets/styles/vant.scss";@import "@/assets/styles/ele.scss";',
                },
                less: {
                    additionalData: '@import "@/assets/styles/index.less";',
                },
            },
        },
        build: {
            rollupOptions: {
                output: {
                    manualChunks: {
                        vue: ['vue', 'pinia', 'vue-router'],
                        elementIcons: ['@element-plus/icons-vue'],
                    },
                    dir: 'dist',
                },
                // 导入自定义的 Rollup 配置
                input: {
                    main: resolve(__dirname, 'index.html'),
                },
            },
        },
    });
};
